<?php
	session_start();
	require('../Model/mysql.php');
	require('../host_config.php');
	error_reporting(E_ALL);
	if ($_SESSION["flag"] != 2) {
        echo "<script language='javascript'>";
        echo "alert('You have no permission to read this page!');";
        echo "history.back();";
        echo "</script>";
        header("location:../login.php");
    }else {
		$mysql = new MySQL();
        $link = $mysql->connect($mysql_host, $mysql_user, $mysql_passwd, $mysql_db);
        $output = array();
        $count = 0;
    	$name = $_POST["searchField"];
    	$name = mysql_real_escape_string($name);
		//$sql = "SELECT * FROM android WHERE all_name = '$name'";
		//clean name_temp table
		//$sql = "DELETE FROM name_temp";
		//$resutl = mysql_query($sql, $link);
		//search
		$sql = "SELECT fullname, shopid FROM Shops";
		$result = mysql_query($sql, $link) or die("Execute SQL command failed!");
		while ($row = mysql_fetch_row($result)) {
			$str1 = $row[0];
			if (preg_match("/".$name."/i", $str1)) {
				//if match insert into user_temp table
				$output[$count] = $row[1];
				$count++;
				//$sql = "INSERT INTO name_temp VALUES(NULL, '$row[0]')";
				//$mysql->query($sql, $link);
				
			}
		}
		/*$number = mysql_num_fields($result);	//????*/
		//判斷name_temp有沒有新增資料
		if ($count == 0) {
			echo "<script type='text/javascript'>
			alter('查無此店');
			</script>";
		//}else if ($count == 1) {
			//$sql = "SELECT all_name FROM name_temp";
			//$row = $mysql->query_row($sql, $link);
			//$sql = "SELECT id FROM android WHERE all_name = '$row[0]'";
			//$sql = "SELECT shopid FROM Shops WHERE fullname = '$row[0]'";
			//$row = $mysql->query_row($sql, $link);
			//header("location:test.php?id=$row[0]");
		}else {
			$_SESSION["output"] = $output;
			header("location:name_temp.php?count=$count");
		}
		
    	/*$row = mysql_fetch_row($result);
		if ($row != NULL) {
			header("location:test.php?id=$row[0]");
		}else {
			echo "<script type='text/javascript'
			alter('查無此店');
			</script>";
			//header("location:main.php");
		}*/
	}
?>
